package com.timmy.mvpdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.timmy.mvpdemo.presenter.AbstractMvpPersenter;
import com.timmy.mvpdemo.base.IMvpBaseView;

/**
 * Created by Timmy on 2017/12/11.
 * * @description MvpActivity
 * 指定子类具体的View必须继承自IMvpBaseView4
 * 指定子类具体的Presenter必须继承自AbstractMvpPersenter4
 */

public abstract class AbstractMvpActivity<V extends IMvpBaseView, P extends AbstractMvpPersenter<V>>
        extends AppCompatActivity implements IMvpBaseView {

    private P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (mPresenter == null) {
            mPresenter = createrPresenter();
        }

        if (mPresenter == null) {
            throw new NullPointerException("p 层不可以为空!");
        }

        // 绑定view
        mPresenter.attachMvpView((V) this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            // 解除绑定
            mPresenter.detachMvpView();
        }
    }

    /**
     * 创建 P 层
     *
     * @return
     */
    protected abstract P createrPresenter();

    public P getPresenter() {

        return mPresenter;
    }
}
